Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
How to use class methods without useing "type": "module" in node js

js developer. I am making express and jade web app. Now I am trying to call class method in index.router.js from login.controller.js, I have been looking for a way to call the method without adding "type": "module" in package.json. This is because my teacher said I should avoid using it for now. I have been looking for a solution for this, but I haven't figured out how. Thanks for reading!

Here's my code:

//This is index.route.js

const express = require('express');
const app     = express();
const IndexRouter  = express.Router();
const LoginController = require('../controllers/login.controller')

IndexRouter.use('/', LoginController.login())

module.exports = IndexRouter
//This is login.controller.js

module.exports = class LoginController{
    async login (req,res) {
        res.send("Login Page")
    }
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

  1. Create Class Objects and use class objects to access class methods

    In index.route.js

    const express = require('express');
    const app = express();
    const IndexRouter = express.Router();
    const LoginController = require('../controllers/login.controller')
    const loginController = new LoginController();
    IndexRouter.use('/', loginController.login())
    
    module.exports = IndexRouter
    

    In login.controller.js

    class LoginController {
        async login(req, res) {
            res.send("Login Page")
        }
    }
    
    module.exports = LoginController;
    

    For Ex.

        class LoginController {
            login(req, res) {
                console.log("LoginController Login");
            }
        }
    
        const loginController = new LoginController();
        loginController.login();

  2. Or can directly access Login function without using class

    In index.route.js

    const express = require('express');
    const app = express();
    const IndexRouter = express.Router();
    const { login } = require('../controllers/login.controller')
    IndexRouter.use('/', login()); // or use('/', login) if using middleware
    
    module.exports = IndexRouter
    

    In login.controller.js

    async function login(req, res) {
        res.send("Login Page")
    }
    
    module.exports = {
        login
    };
    
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!